#include "asm.h"
#include "timer.h"
#include "interrupts.h"
#include "pic.h"
#include "proc.h"

u32 ticks;

void timer_isr() {
  return;
}

void init_timer(int frequency) {
  set_timer(frequency);
  register_isr(0x20, timer_isr);
  enable_irq(TIMER);
}

void set_timer(int frequency) {
  u32 divisor = 1193180 / frequency;
  // Send the command byte.
  outb(0x43, 0x36);

  // Divisor has to be sent byte-wise, so split here into upper/lower bytes.
  u8 l = (u8)(divisor & 0xFF);
  u8 h = (u8)( (divisor>>8) & 0xFF );
  
   // Send the frequency divisor.
  outb(0x40, l);
  outb(0x40, h);
}

void timer_wait(u32 t) {
  ticks = 0;
  
  while(ticks < t)
    ;
}

